package com.course.server.service;

import com.course.server.domain.eacher;
import com.course.server.domain.eacherExample;
import com.course.server.dto.eacherDto;
import com.course.server.dto.PageDto;
import com.course.server.mapper.eacherMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class eacherService {
    @Resource
    private eacherMapper eacherMapper;

    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        eacherExample eacherExample = new eacherExample();
        List<eacher> eacherList = eacherMapper.selectByExample(eacherExample);
        PageInfo<eacher> pageInfo = new PageInfo<>(eacherList);
        pageDto.setTotal(pageInfo.getTotal());
        List<eacherDto> eacherDtoList = CopyUtil.copyList(eacherList, eacherDto.class);
        pageDto.setList(eacherDtoList);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(eacherDto eacherDto) {
        eacher eacher = CopyUtil.copy(eacherDto, eacher.class);
        if (StringUtils.isEmpty(eacherDto.getId())) {
            this.insert(eacher);
        } else {
            this.update(eacher);
        }
    }

    /**
     * 新增
     */
    private void insert(eacher eacher) {
        eacher.setId(UuidUtil.getShortUuid());
        eacherMapper.insert(eacher);
    }

    /**
     * 更新
     */
    private void update(eacher eacher) {
        eacherMapper.updateByPrimaryKey(eacher);
    }

    /**
     * 删除
     */
    public void delete(String id) {
        eacherMapper.deleteByPrimaryKey(id);
    }
}